package com.xiaojie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaojie.entity.Admin;
import com.xiaojie.dao.AdminMapper;
import com.xiaojie.service.IAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;

import java.nio.charset.StandardCharsets;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaojie
 * @since 2022-07-27
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {

    @Override
    public Admin login(Admin admin) {
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("username",admin.getUsername());
        Admin admin1 = baseMapper.selectOne(wrapper);
        if (ObjectUtils.isEmpty(admin1)){
            throw new IllegalThreadStateException("用户名或密码错误");
        }
        String password = DigestUtils.md5DigestAsHex(admin.getPassword().getBytes(StandardCharsets.UTF_8));
        if (!StringUtils.equals(password,admin1.getPassword())){
            throw new IllegalThreadStateException("密码错误");
        }
        return admin1;
    }
}
